package com.lcc.lynx.base.system.service.impl;

import com.lcc.lynx.base.system.mapper.SysRoleMapper;
import com.lcc.lynx.base.system.pojo.dv.SysRoleDV;
import com.lcc.lynx.base.system.pojo.dv.SysRoleMenuDV;
import com.lcc.lynx.base.system.service.SysRoleMenuService;
import com.lcc.lynx.base.system.service.SysRoleService;
import com.lcc.lynx.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统角色服务实现
 * @auth lcc
 * @date 2025-01-01
 */
@Service
@Transactional
@AllArgsConstructor
public class SysRoleServiceImpl implements SysRoleService {

    private SysRoleMapper mapper;

    private SysRoleMenuService roleMenuService;

    @Override
    public SysRoleMapper getMapper() {
       return mapper;
    }

    @Override
    public void addAndMenu(SysRoleDV dv) {
        this.insert(dv);
        if(dv.getMenuList() != null) {
            for (SysRoleMenuDV menuDV : dv.getMenuList()) {
                menuDV.setRoleId(dv.getRoleId());
            }
        }
        roleMenuService.insertList(dv.getMenuList());
    }

    @Override
    public void updateAndMenu(SysRoleDV dv) {
        this.updateById(dv);
        this.updateMenu(dv);
    }

    @Override
    public void updateMenu(SysRoleDV dv) {
        if(dv.getMenuList() == null) {
            throw new BusinessException("菜单列表不能为空");
        }
        SysRoleMenuDV delete = new SysRoleMenuDV();
        delete.setRoleId(dv.getRoleId());
        roleMenuService.delete(delete);
        for (SysRoleMenuDV menuDV : dv.getMenuList()) {
            menuDV.setRoleId(dv.getRoleId());
        }
        roleMenuService.insertList(dv.getMenuList());
    }
}